package com.pizidea.imagepicker.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.pizidea.imagepicker.ImagePresenter;
import com.pizidea.imagepicker.UilImagePresenter;
import com.pizidea.imagepicker.bean.ImageItem;
import com.pizidea.imagepicker.widget.TouchImageView;

import net.neiquan.applibrary.utils.ImageUtil;

import org.haitao.common.utils.AppLog;

/**
 * Author：邓勇军
 * English Name：Deng,The Sky Fucker!
 * Email：dandycoder@126.com
 * Date：2017/5/26
 * Version：1.0
 */

public class SinglePreviewFragment extends Fragment {
    public static final String KEY_URL = "key_url";
    private static Activity mContext;
    private static boolean enableSingleTap;
    private static ImagePresenter mImagePresenter;
    private TouchImageView imageView;
    private String url;
    private static ImagePreviewFragment.OnImageSingleTapClickListener onImageSingleTapClickListener;

    public static SinglePreviewFragment getInstance(Activity mContext, boolean enableSingleTap, ImagePresenter mImagePresenter, ImagePreviewFragment.OnImageSingleTapClickListener onImageSingleTapClickListener) {
        SinglePreviewFragment.mContext = mContext;
        SinglePreviewFragment.enableSingleTap = enableSingleTap;
        SinglePreviewFragment.mImagePresenter = mImagePresenter;
        SinglePreviewFragment.onImageSingleTapClickListener=onImageSingleTapClickListener;
        return new SinglePreviewFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();

        ImageItem imageItem = bundle.getParcelable(KEY_URL);

        url = imageItem.path;

        AppLog.e("=====current show image path:" + url);

        imageView = new TouchImageView(mContext);
        imageView.setBackgroundColor(0xff000000);
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        imageView.setLayoutParams(params);
        imageView.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
                AppLog.e("================onSingleTapConfirmed111============");
                onImageSingleTapClickListener.onImageSingleTap(e);
//                if (enableSingleTap) {
//                    if (mContext instanceof ImagePreviewFragment.OnImageSingleTapClickListener) {
//                        AppLog.e("================onSingleTapConfirmed222============");
//                        ((ImagePreviewFragment.OnImageSingleTapClickListener) mContext).onImageSingleTap(e);
//                    }
//                }
                return false;
            }

            @Override
            public boolean onDoubleTapEvent(MotionEvent e) {
                return false;
            }

            @Override
            public boolean onDoubleTap(MotionEvent e) {
                return false;
            }

        });

        //display the image with your own ImageLoader
        if (url.contains("http") || url.contains("https")) {
            ImageUtil.loadPicNet(url, imageView);
        } else {
            ((UilImagePresenter) mImagePresenter).onPresentImage2(imageView, url, imageView.getWidth());
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return imageView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mContext != null) {
            mContext = null;
        }
    }


}
